package com.zhihuichongcheng.application.ui.openclass.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.openclass.SubCategory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/11/2.
 */

public class OpenClassSubCategoryAdapter extends RecyclerView.Adapter<OpenClassSubCategoryAdapter.ViewHold> {
    private Context mContext;
    private OnItemOnclick<SubCategory> onItemOnclick;
    private List<SubCategory> mListData = new ArrayList<>();
    private int mIndex;
    public OpenClassSubCategoryAdapter(Context context)
    {
        this.mContext = context;
    }

    public void setPosition(int position)
    {
        this.mIndex = position;
    }
    public void setData(List<SubCategory> list)
    {
        this.mListData = list;
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_openclass_sub_category,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final SubCategory bean = mListData.get(position);
        holder.mTvName.setText(bean.getCategoryName());
        if(mIndex ==position)
        {
            holder.mViewBottom.setVisibility(View.VISIBLE);
            holder.mTvName.setTextColor(mContext.getResources().getColor(R.color.activity_color));
        }
        else
        {
            holder.mViewBottom.setVisibility(View.GONE);
            holder.mTvName.setTextColor(mContext.getResources().getColor(R.color.color_999));
        }
//        holder.view.setLayoutParams(new LinearLayout.LayoutParams(DeviceUtil.dip2px(mContext,80), ViewGroup.LayoutParams.WRAP_CONTENT));
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemOnclick!=null)
                {
                    onItemOnclick.onClick(bean,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setOnItemOnclick(OnItemOnclick<SubCategory> onItemOnclick) {
        this.onItemOnclick = onItemOnclick;
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private TextView mTvName;
        private View mViewBottom;
        private View view;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mTvName = (TextView)itemView.findViewById(R.id.tv_name);
            mViewBottom = (View)itemView.findViewById(R.id.view_bottom);
        }
    }
}
